<?php

namespace App\Http\Controllers;

use App\Models\Friends;
use App\Models\Member;
use App\Models\Message;
use Illuminate\Http\Request;

class MemberController extends Controller
{
    //会员列表
    public function memberList()
    {
        $data = Member::all();

        return response()->json([
            'code'=>1,
            'msg' =>'',
            'data' => $data
        ]);
    }

    //会员信息
    public function memberInfo(Request $request)
    {
        global $g_webid;
        $mem_id = $request->input('mem_id');
        if($mem_id) {
            $data = Member::find($mem_id);

            $data->company              = '深圳腾讯网络科技有限公司';
            $data->job                  = '执行总裁';
            $data->industry             = '计算机软件';
            $data->address              = '深圳市';
            $data->set_up_time          = '今年成立';
            $data->registered_capital   = '200万人民币';

            if($data) {
                $is_login_user = 0;
                if($mem_id == $g_webid){
                    $is_login_user = 1;
                }
                return response()->json([
                    'code' => 1,
                    'msg' => '',
                    'data' => $data,
                    'is_login_user' => $is_login_user
                ]);
            }else{
                return response()->json([
                    'code' => 0,
                    'msg' => '',
                    'data' => []
                ]);
            }
        }else{
            return response()->json([
                'code'=>0,
                'msg' =>'缺少用户id',
                'data' => ''
            ]);
        }
    }

    //会员好友列表
    public function memberFriendList()
    {

    }

    public function getCurrentUserInfo(){
        global $g_webid;
        $member = Member::where('id' , $g_webid)->first();

        return response()->json([
            'code' => 1,
            'msg' => '',
            'data' => $member
        ]);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addMessage(Request $request){
        $validation = \Validator::make($request->all(), [
            'from_user' => 'required',
            'to_user'   => 'required',
            'content'   => 'required',
            'type'      => 'required',
        ], [
            'from_user.required'    => "缺少from_user参数",
            'to_user.required'      => "缺少to_user参数",
            'content.required'      => "缺少content参数",
            'type.required'         => "缺少type参数",
        ]);
        if ($validation->fails()) {
            $error = $validation->errors()->all();
            return response()->json(array('code' => 0, 'msg' => $error[0]));
        }

        $data = $request->all();
        $data['created_time'] = date('Y-m-d H:i:s' , $_SERVER['REQUEST_TIME']);
        $data['content'] = trim($data['content']);

        $messageModel = new Message();
        if($messageModel->saveData($data)){
            return response()->json(array('code' => 1, 'msg' => '成功'));
        } else{
            return response()->json(array('code' => 0, 'msg' => '失败'));
        }
    }

    public function getMessage($from, $to, Request $request){

        $list = Message::where('from_user' , $from)
            ->where('to_user' , $to)
            ->paginate();

        return response()->json(array('code' => 1, 'msg' => '成功' , 'data' => $list));
    }

    public function getFriendList($uid , Request $request){
        $friendModel = new Friends();
        $memModel = new Member();

        $list = \DB::table($friendModel->getTable() . ' as f')
            ->leftJoin($memModel->getTable() . ' as m' , 'f.friends_id' , '=' , 'm.id')
            ->where('f.uid' , $uid)
            ->get();
        return response()->json(array('code' => 1, 'msg' => '成功' , 'data' => $list));
    }

}
